/*
 * jdbc-trace-wrapper
 * Copyright (C) 2010. The jdbc-trace-wrapper Project
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package nz.jdbcwrapper;

import java.sql.SQLWarning;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Set;

abstract class WarningHolder
{
  final Set warnings = new HashSet();
  final String prefix;
  final long id = Util.id();
  
  WarningHolder(String parentPrefix)
  {
    this.prefix = parentPrefix + "[" + id + "]";
  }

  abstract SQLWarning getProxyWarnings() throws SQLException;

  protected void checkWarnings()
  {
    try
    {
      SQLWarning firstWarning = getProxyWarnings();
      for (SQLWarning warning = firstWarning;
           warning != null;
           warning = warning.getNextWarning()
      )
      {
        if (! warnings.contains(warning))
        {
          warnings.add(warning);
          Util.trace(prefix + " ++++++ " + warning.toString());
        }
      }
      
      for (SQLException excp = firstWarning;
           excp != null;
           excp = excp.getNextException()
      )
      {
        if (! warnings.contains(excp))
        {
          warnings.add(excp);
          Util.trace(prefix + " ===== " + excp.toString());
        }
      }
    }
    catch (SQLException e)
    {
      Util.trace(prefix + "****** Exception during warning checks: " + e);
    }
  }
}
